{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. np.concatenate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "沿轴0拼接：\n",
      " [[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "沿轴1拼接：\n",
      " [[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.arange(1, 7).reshape(2, 3)\n",
    "arr2 = np.arange(7, 13).reshape(2, 3)\n",
    "concatenate1 = np.concatenate((arr1, arr2))\n",
    "print(\"沿轴0拼接：\\n\", concatenate1)\n",
    "concatenate2 = np.concatenate((arr1, arr2), axis=1)\n",
    "print(\"沿轴1拼接：\\n\", concatenate2)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. np.vstack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "竖向拼接：\n",
      " [[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "vstack1 = np.vstack((arr1, arr2))\n",
    "print(\"竖向拼接：\\n\", vstack1)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. np.hstack"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "横向拼接：\n",
      " [[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "hstack1 = np.hstack((arr1, arr2))\n",
    "print(\"横向拼接：\\n\", hstack1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. np.split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.7310142  -0.07038107 -0.72651457]\n",
      " [ 0.65815392  0.22698868  0.36520137]\n",
      " [ 0.46797956 -0.33240395  0.09147387]\n",
      " [-1.23166319 -1.19977704 -0.7373072 ]\n",
      " [ 0.09072937 -0.39859722 -0.30113282]]\n",
      "[array([[-0.7310142 , -0.07038107, -0.72651457]]), array([[ 0.65815392,  0.22698868,  0.36520137],\n",
      "       [ 0.46797956, -0.33240395,  0.09147387]]), array([[-1.23166319, -1.19977704, -0.7373072 ],\n",
      "       [ 0.09072937, -0.39859722, -0.30113282]])]\n",
      "[array([[-0.7310142 ],\n",
      "       [ 0.65815392],\n",
      "       [ 0.46797956],\n",
      "       [-1.23166319],\n",
      "       [ 0.09072937]]), array([[-0.07038107],\n",
      "       [ 0.22698868],\n",
      "       [-0.33240395],\n",
      "       [-1.19977704],\n",
      "       [-0.39859722]]), array([[-0.72651457],\n",
      "       [ 0.36520137],\n",
      "       [ 0.09147387],\n",
      "       [-0.7373072 ],\n",
      "       [-0.30113282]])]\n",
      "[array([[-0.7310142 , -0.07038107, -0.72651457]]), array([[0.65815392, 0.22698868, 0.36520137]]), array([[ 0.46797956, -0.33240395,  0.09147387]]), array([[-1.23166319, -1.19977704, -0.7373072 ]]), array([[ 0.09072937, -0.39859722, -0.30113282]])]\n"
     ]
    }
   ],
   "source": [
    "arr3 = np.random.randn(5, 3)\n",
    "print(arr3)\n",
    "split1 = np.split(arr3, [1, 3])\n",
    "print(split1)\n",
    "split2 = np.split(arr3, [1, 2], axis=1)\n",
    "print(split2)\n",
    "split3 = np.split(arr3, 5)\n",
    "print(split3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. np.array_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[-0.7310142 , -0.07038107, -0.72651457],\n",
      "       [ 0.65815392,  0.22698868,  0.36520137]]), array([[ 0.46797956, -0.33240395,  0.09147387],\n",
      "       [-1.23166319, -1.19977704, -0.7373072 ]]), array([[ 0.09072937, -0.39859722, -0.30113282]])]\n"
     ]
    }
   ],
   "source": [
    "array_split1 = np.array_split(arr3, 3)\n",
    "print(array_split1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "6. np.hsplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[-0.7310142 ],\n",
       "        [ 0.65815392],\n",
       "        [ 0.46797956],\n",
       "        [-1.23166319],\n",
       "        [ 0.09072937]]),\n",
       " array([[-0.07038107],\n",
       "        [ 0.22698868],\n",
       "        [-0.33240395],\n",
       "        [-1.19977704],\n",
       "        [-0.39859722]]),\n",
       " array([[-0.72651457],\n",
       "        [ 0.36520137],\n",
       "        [ 0.09147387],\n",
       "        [-0.7373072 ],\n",
       "        [-0.30113282]])]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.hsplit(arr3, [1, 2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "7. np.vsplit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[-0.7310142 , -0.07038107, -0.72651457]]),\n",
       " array([[ 0.65815392,  0.22698868,  0.36520137],\n",
       "        [ 0.46797956, -0.33240395,  0.09147387]]),\n",
       " array([[-1.23166319, -1.19977704, -0.7373072 ],\n",
       "        [ 0.09072937, -0.39859722, -0.30113282]])]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.vsplit(arr3, [1, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "8. repeat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 1 1 1 2 2 2]\n",
      "[0 0 1 1 2 2 2]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[0.36347221, 0.40422413, 0.40422413, 0.78529558, 0.78529558,\n",
       "        0.78529558],\n",
       "       [0.20752238, 0.3397467 , 0.3397467 , 0.67781315, 0.67781315,\n",
       "        0.67781315],\n",
       "       [0.20557792, 0.45454487, 0.45454487, 0.45621052, 0.45621052,\n",
       "        0.45621052],\n",
       "       [0.56731853, 0.49498986, 0.49498986, 0.14789861, 0.14789861,\n",
       "        0.14789861]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.arange(3)\n",
    "a = arr.repeat(3)\n",
    "print(a)\n",
    "b = arr.repeat([2, 2, 3])\n",
    "print(b)\n",
    "arr = np.random.rand(4, 3)\n",
    "arr.repeat(2, axis=1)\n",
    "arr.repeat([1, 2, 3, 4], axis=0)\n",
    "arr.repeat([1, 2, 3], axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.concatenate\n",
    "假设你正在为一个游戏设计一个积分系统，需要将两个玩家的得分数组合并。玩家1的得分数组是arr1 = np.array([[1, 2, 3], [4, 5, 6]])，玩家2的得分数组是arr2 = np.array([[7, 8, 9], [10, 11, 12]])。请使用np.concatenate函数将这两个数组合并，并打印合并后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1:\n",
      " [[1 2 3]\n",
      " [4 5 6]]\n",
      "arr2:\n",
      " [[ 7  8  9]\n",
      " [10 11 12]]\n",
      "组合后的数组为：\n",
      " [[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "arr2 = np.array([[7, 8, 9], [10, 11, 12]])\n",
    "print(\"arr1:\\n\", arr1)\n",
    "print(\"arr2:\\n\", arr2)\n",
    "concatenate_arr = np.concatenate((arr1, arr2), axis=0)\n",
    "print(\"组合后的数组为：\\n\", concatenate_arr)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.vstack\n",
    "在一个实验中，你记录了两组不同条件下的测量数据。第一组数据是arr1 = np.arange(1, 7).reshape(2, 3)，第二组数据是arr2 = np.arange(7, 13).reshape(2, 3)。请使用np.vstack函数将这两组数据沿着轴0合并，并打印合并后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1:\n",
      " [[1 2 3]\n",
      " [4 5 6]]\n",
      "arr2:\n",
      " [[ 7  8  9]\n",
      " [10 11 12]]\n",
      "竖向合并的数组为：\n",
      " [[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.arange(1, 7).reshape(2, 3)\n",
    "arr2 = np.arange(7, 13).reshape(2, 3)\n",
    "print(\"arr1:\\n\", arr1)\n",
    "print(\"arr2:\\n\", arr2)\n",
    "vstack_arr = np.vstack((arr1, arr2))\n",
    "print(\"竖向合并的数组为：\\n\", vstack_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.hstack\n",
    "你正在分析两个不同时间段的交通流量数据。数据arr1和arr2分别表示上午和下午的车流量，格式为np.array([[车流量1], [车流量2]])。请使用np.hstack函数将这两个数组沿着轴1合并，并打印合并后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1:\n",
      " [[80 53 82 45]\n",
      " [38 73 21 91]\n",
      " [11 61 99  2]\n",
      " [49 71 23 15]\n",
      " [72 58 97 57]]\n",
      "arr2:\n",
      " [[57 47 63 99]\n",
      " [35  9 10  8]\n",
      " [46  5 45 50]\n",
      " [ 0 77 63 83]\n",
      " [85 46 35 11]]\n",
      "沿轴1合并的结果为：\n",
      " [[80 53 82 45 57 47 63 99]\n",
      " [38 73 21 91 35  9 10  8]\n",
      " [11 61 99  2 46  5 45 50]\n",
      " [49 71 23 15  0 77 63 83]\n",
      " [72 58 97 57 85 46 35 11]]\n",
      "使用concatenate合并轴1的结果：\n",
      " [[80 53 82 45 57 47 63 99]\n",
      " [38 73 21 91 35  9 10  8]\n",
      " [11 61 99  2 46  5 45 50]\n",
      " [49 71 23 15  0 77 63 83]\n",
      " [72 58 97 57 85 46 35 11]]\n",
      "使用concatenate合并轴0的结果：\n",
      " [[80 53 82 45]\n",
      " [38 73 21 91]\n",
      " [11 61 99  2]\n",
      " [49 71 23 15]\n",
      " [72 58 97 57]\n",
      " [57 47 63 99]\n",
      " [35  9 10  8]\n",
      " [46  5 45 50]\n",
      " [ 0 77 63 83]\n",
      " [85 46 35 11]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr1 = np.random.randint(low=0, high=100, size=(5, 4))\n",
    "arr2 = np.random.randint(low=0, high=100, size=(5, 4))\n",
    "print(\"arr1:\\n\", arr1)\n",
    "print(\"arr2:\\n\", arr2)\n",
    "hstack_arr = np.hstack((arr1, arr2))\n",
    "print(\"沿轴1合并的结果为：\\n\", hstack_arr)\n",
    "concatenate_arr = np.concatenate((arr1, arr2), axis=1)\n",
    "print(\"使用concatenate合并轴1的结果：\\n\", concatenate_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.split\n",
    "你有一个包含5天温度记录的数组arr = np.random.randn(5,3)，需要将这些数据分割成两个数组，一个包含前3天的数据，另一个包含后2天的数据。请使用np.split函数按0轴分割数组，并打印分割后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr的结果：\n",
      " [[-1.57403969 -1.19355732 -1.06072964]\n",
      " [-0.15490787 -0.49132471 -0.57361058]\n",
      " [ 2.17458466  0.01847121  0.68474424]\n",
      " [-0.87789356 -0.41377409  0.77579987]\n",
      " [-1.36966217 -1.17568635  0.99963616]]\n",
      "拆分的第一个为：\n",
      " [[-1.57403969 -1.19355732 -1.06072964]\n",
      " [-0.15490787 -0.49132471 -0.57361058]\n",
      " [ 2.17458466  0.01847121  0.68474424]]\n",
      "拆分第第二个为：\n",
      " [[-0.87789356 -0.41377409  0.77579987]\n",
      " [-1.36966217 -1.17568635  0.99963616]]\n",
      "拆分的第一个为：\n",
      " [[-1.57403969 -1.19355732]\n",
      " [-0.15490787 -0.49132471]\n",
      " [ 2.17458466  0.01847121]\n",
      " [-0.87789356 -0.41377409]\n",
      " [-1.36966217 -1.17568635]]\n",
      "拆分第第二个为：\n",
      " [[-1.06072964]\n",
      " [-0.57361058]\n",
      " [ 0.68474424]\n",
      " [ 0.77579987]\n",
      " [ 0.99963616]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randn(5, 3)\n",
    "print(\"arr的结果：\\n\", arr)\n",
    "arr1, arr2 = np.split(arr, [3])\n",
    "print(\"拆分的第一个为：\\n\", arr1)\n",
    "print(\"拆分第第二个为：\\n\", arr2)\n",
    "arr1, arr2 = np.split(arr, [2], axis=1) # 拓展分割轴1\n",
    "print(\"拆分的第一个为：\\n\", arr1)\n",
    "print(\"拆分第第二个为：\\n\", arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.hsplit\n",
    "在一个市场调研项目中，你收集了四个商场一周内不同时间段顾客的流量数据，数据存储在二维数组arr = np.random.randint(1, 100, (4, 7))中，其中每一行代表一个商场，每一列代表一天。请使用np.hsplit函数拆分出工作日和周末的顾客流量数据，并打印分割后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr的值为：\n",
      " [[25 74 45 94 26 94 40]\n",
      " [63 94 94 42  5  4 17]\n",
      " [ 7 35 67 97 25  9 75]\n",
      " [75 76 37 84 99 92 71]]\n",
      "工作日数据：\n",
      " [[25 74 45 94 26]\n",
      " [63 94 94 42  5]\n",
      " [ 7 35 67 97 25]\n",
      " [75 76 37 84 99]]\n",
      "周末数据：\n",
      " [[94 40]\n",
      " [ 4 17]\n",
      " [ 9 75]\n",
      " [92 71]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.randint(1, 100, (4, 7))\n",
    "print(\"arr的值为：\\n\", arr)\n",
    "week_arr, weekend_arr = np.hsplit(arr, [5])\n",
    "print(\"工作日数据：\\n\", week_arr)\n",
    "print(\"周末数据：\\n\", weekend_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.vsplit\n",
    "在一个生物学实验中，你记录了4组不同植物的生长情况，每组包含3次测量。数据存储在数组arr = np.random.rand(4,3)中。请使用np.vsplit函数将这个数组分割成4个数组，每个数组包含1组植物的生长数据，并打印分割后的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr的值为：\n",
      " [[0.24708287 0.24691406 0.95324849]\n",
      " [0.09668854 0.74076635 0.86994276]\n",
      " [0.76695202 0.38519038 0.42730349]\n",
      " [0.41668892 0.55165066 0.79303886]]\n",
      "四组的结果分别为：\n",
      "[[0.24708287 0.24691406 0.95324849]]\n",
      "[[0.09668854 0.74076635 0.86994276]]\n",
      "[[0.76695202 0.38519038 0.42730349]]\n",
      "[[0.41668892 0.55165066 0.79303886]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.random.rand(4, 3)\n",
    "print(\"arr的值为：\\n\", arr)\n",
    "arr1, arr2, arr3, arr4 = np.vsplit(arr, 4)\n",
    "print(f\"四组的结果分别为：\\n{arr1}\\n{arr2}\\n{arr3}\\n{arr4}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# np.repeat\n",
    "数组arr = np.array([110, 120, 130, 135, 150])表示一个宿舍5名学生的体重。宿舍调整，增加了2个130斤的同学，一个150斤的同学，请使用np.repeat函数统计现在宿舍成员的体重，并打印结果数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数组的值为：\n",
      " [110 120 130 135 150]\n",
      "更新后的数组值为：\n",
      " [110 120 130 130 130 135 150 150]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "arr = np.array([110, 120, 130, 135, 150])\n",
    "print(\"数组的值为：\\n\", arr)\n",
    "repeated_arr = arr.repeat([1, 1, 3, 1, 2])\n",
    "print(\"更新后的数组值为：\\n\", repeated_arr)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python-data-analysis",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
